import { postImg } from 'services/upImg';
import { Toast } from 'antd-mobile';
import router from 'umi/router';
export default {
  namespace: 'upImgModel',
  state: {
    baseData:'',
    defaultImg:'',
  },
  effects: {
    *upImg({ payload,callback }, { call, put }) {
      Toast.loading('提交中...',0)
      const response = yield call(postImg, payload);
      let reg = new RegExp(/0000$/, 'i')
      if(reg.test(response.code)) {
        yield put({
          type: 'save',
          payload: response.data
        });
        yield callback('success', '上传图片成功')
      }else{
        let { message="图片上传失败"} = response
        yield callback('error',message)
      }
    },
    *saveDefaultImg({ payload }, { put,select }) {
      const {defaultImg} = yield select(_=>_.upImgModel);
      if(!defaultImg){
        yield put({
          type: 'save',
          payload:{
            defaultImg:payload.defaultImg
          }
        });
      }
    },
    *delDefaultImg({ payload }, { put }) {
      yield put({
          type: 'save',
          payload:{
            defaultImg:payload.defaultImg
          }
        });
    
    }
  },
  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      }
    }
  },

};
